<?php

namespace app\admin\controller;

use app\exception\ParamException;
use app\model\AdminModel;
use app\model\UploadFileModel;
use support\Request;
use Tinywan\Storage\Storage;

/**
 * 后台无需登陆控制器
 */
class PublicController
{
    
    //登陆
    public function Login(Request $request){

        $params = $request->post();

        if(!$params['username'] || !$params['password']){
            return error('参数错误');
        }

        //登陆
        $admin = AdminModel::where("username",$params['username'])->find();

        if(!$admin) return error('用户不存在!');

        if(get_password($params['password']) !== $admin->password){
            return error('密码错误');
        }

        if($admin->status !== 1){
            return error('用户被禁用');
        }

        //登陆成功
        $admin->last_time = time();
        $admin->last_ip = $request->getRealIp();
        $admin->save();

        //获取token
        $token = AdminModel::getToken($admin->id);

        return success('ok',$token);
    }


    //文件上传(单文件)
    public function uploadOne(Request $request){
        $type = 0;
        //最大上传大小
        $maxsize = bcmul(30,(1024*1024),0);
        $files = $request->file();

        if(count($files) > 1){
            return error('只能上传一个文件');
        }
        
        //验证文件类型 、 文件大小等
        foreach ($request->file() as $key => $spl_file) {

            if(!$spl_file->isValid()){
                return error('文件已失效');
            }

            if($spl_file->getSize() > $maxsize){
                return error('超过限制文件大小');
            }
        
        }

        if($type == 0){
            //本地上传
            try{
                Storage::config('local');
                $res = Storage::uploadFile();
            }catch(\Throwable $e){
                return error('上传失败,'.$e->getMessage());
            }
        }else if($type == 1){
            //阿里云上传
            try{
                Storage::config('oss');
                $res = Storage::uploadFile();

            }catch(\Throwable $e){

                return error('上传失败,'.$e->getMessage());
            }

        }else if($type == 2){
            try{

                Storage::config('qiniu');
                $res = Storage::uploadFile();

            }catch(\Throwable $e){

                return error('上传失败,'.$e->getMessage());
            }

        }

        //添加文件上传记录
        foreach($res as $k=>$v){
            UploadFileModel::upload($v);

            //隐藏完整路径
            unset($res[$k]['save_path']);
        }

        return success('ok',$res[0]['url']);

    }

    //文件上传(多文件)
    public function upload(Request $request){
        //0:本地上传 1:阿里云 2:七牛云
        $type = 0;

        //允许的文件扩展名
        //$upload_info_extensions = explode(",",trim($upload_info['extensions'],","));

        //最大上传大小
        $maxsize = bcmul(30,(1024*1024),0);

        foreach ($request->file() as $key => $spl_file) {

            //字节/1024/1024 = Mb

            if(!$spl_file->isValid()){
                return error($spl_file->getUploadName().'已失效');
            }

            // 验证文件后缀
            // if(!in_array($spl_file->getUploadExtension(),$upload_info_extensions)){
            //     return api_error($spl_file->getUploadExtension().'文件不允许上传');
            // }

            if($spl_file->getSize() > $maxsize){
                return error($spl_file->getUploadName().'大小超出上传限制');
            }

            // var_export($spl_file->isValid()); // 文件是否有效，例如ture|false
            // var_export($spl_file->getUploadExtension()); // 上传文件后缀名，例如'jpg'
            // var_export($spl_file->getUploadMineType()); // 上传文件mine类型，例如 'image/jpeg'
            // var_export($spl_file->getUploadErrorCode()); // 获取上传错误码，例如 UPLOAD_ERR_NO_TMP_DIR UPLOAD_ERR_NO_FILE UPLOAD_ERR_CANT_WRITE
            // var_export($spl_file->getUploadName()); // 上传文件名，例如 'my-test.jpg'
            // var_export($spl_file->getSize()); // 获得文件大小，例如 13364，单位字节
            // var_export($spl_file->getPath()); // 获得上传的目录，例如 '/tmp'
            // var_export($spl_file->getRealPath()); // 获得临时文件路径，例如 `/tmp/workerman.upload.SRliMu`        
        }

        if($type == 0){
            //本地上传
            try{
                Storage::config('local');
                $res = Storage::uploadFile();
            }catch(\Throwable $e){
                return error('上传失败,'.$e->getMessage());
            }
        }else if($type == 1){
            //阿里云
            try{
                Storage::config('oss');
                $res = Storage::uploadFile();
            }catch(\Throwable $e){
                return error('上传失败'.$e->getMessage());
            }
        }else if($type == 2){
            //七牛云
            try{
                Storage::config('qiniu');
                $res = Storage::uploadFile();
            }catch(\Throwable $e){
                return error('上传失败'.$e->getMessage());
            }

        }

        //添加文件上传记录
        foreach($res as $k=>$v){
            UploadFileModel::upload($v);

            //隐藏完整路径
            unset($res[$k]['save_path']);
        }

        return success('ok',$res);
    }

}
